<?php
//培训管理
namespace app\admin\controller;
use think\Db;
class TrainController extends CommonController {

    /**
     * 获取试题数据
     */
    public function getInfo(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_subject')
            ->where($where)
            ->order('subject_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_subject')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * ImportTitle 题库展示页面
     * @return 返回展示页面
     */
    public function ImportTitle(){

        return $this->fetch('train/ImportTitle');
    }

    /**
     * insertImportTitle Excel表题库数据导入数据库
     * @return 返回数据存储数据库状态
     */
    public function insertImportTitle(){
        $file_name = $_FILES['excelFile']['name'];//上传的文件名字
        $file_path = $_FILES['excelFile']['tmp_name'];//上传的临时路径
        $file_types = explode('.',$file_name);//切割上传文件名字
        $file_type = $file_types [count ( $file_types ) - 1];//上传的名字后缀
        $first_name = $file_types[0];//去除后缀的文件名
        set_time_limit(0);
        vendor("phpoffice.phpexcel.Classes.PHPExcel.IOFactory");
        if( $file_type == 'xls' ){
            $inputFileType = 'Excel5';    //这个是读 xls的
        }else{
            $inputFileType = 'Excel2007';//这个是计xlsx的
        }

        $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
//       $objPHPExcel = $objReader ->load($file_path,$encode='utf-8');

        $objPHPExcel = $objReader->load($file_path);
//        var_dump($objPHPExcel);die;
        $objWorksheet = $objPHPExcel->getActiveSheet();

        $highestRow = $objWorksheet->getHighestRow();
        $time = date('Y-m-d H:i:s');
        /*循环遍历获取Excel表的数据*/
        $num = 0;
        for ($is=2;$is<=$highestRow;$is++){
            $test = $objPHPExcel->getActiveSheet()->getCell("A".$is)->getValue();
            if($test){
                $num++;
                $data = array();//放用户ID的数组
                $data['subject_question'] = $objPHPExcel->getActiveSheet()->getCell("A".$is)->getValue();//获取A列的值 试题简介
                $data['subject_a'] = $objPHPExcel->getActiveSheet()->getCell("B".$is)->getValue();//获取B列的值  题目
                $data['subject_b'] = $objPHPExcel->getActiveSheet()->getCell("C".$is)->getValue();//获取C列的值  选择题答案A
                $data['subject_c'] = $objPHPExcel->getActiveSheet()->getCell("D".$is)->getValue();//获取D列的值  选择题答案B
                $data['subject_d'] = $objPHPExcel->getActiveSheet()->getCell("E".$is)->getValue();//获取E列的值  选择题答案C
                $data['subject_answer'] = $objPHPExcel->getActiveSheet()->getCell("F".$is)->getValue();//获取F列的值  选择题答案D
                $data['subject_time'] = $time;//获取G列的值  答案
                $res = Db::table('ew_subject')->insert($data);
                if(!$res){
                    $data['status'] = 0;
                    $data['msg'] = "导入到第{$num}条题‘{$data['subject_question']}’时出错，请检查导入数据是否有误";
                    return json_encode($data);
                }
            }
        }
        $data['status'] = 200;
        $data['msg'] = "导入成功，共计导入{$num}条题";
        return json_encode($data);
    }

    /***
     * 编辑页面
     */
    public function updateTrain(){
        $s_id = $_POST['s_id'];
        $data = Db::table('ew_subject')->where('subject_id',$s_id)->find();
        $this->assign('data',$data);
        return $this->fetch('train/updateTrain');

    }
    /***
     * 编辑功能
     */
    public function updateInfo(){
        $where['subject_id'] = $_POST['subject_id'];
        if(trim($_POST['subject_question'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "试题名称不能为空";
            return json_encode($returnData);
        }
        unset($_POST['subject_id']);
        $res = Db::table('ew_subject')->where($where)->update($_POST);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }  else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }

    /**
     * Score 每日答题次数限制
     * @return 返回展示页面
     */
    public function Score(){
        $res = Db::table('ew_personalconfig')
            ->where('id',7)
            ->find();
        $this->assign('res',$res);
        return $this->fetch('train/Score');
    }

    /**
     * changeScore 修改每日答题次数限制
     * @return 返回展示页面
     */
    public function changeScore(){
        $region_name = input('userid');
//        var_dump(input(''));die;
        $res = Db::table('ew_personalconfig')
            ->where('id',7)
            ->update(['userid'=>$region_name]);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
        }
        return json_encode($returnData);
    }
    /**
     * changeScore shanchu
     * @return 返回展示页面
     */
    public function deleteInfo()
    {
        $where['subject_id'] = $_POST['s_id'];
        $res = Db::table('ew_subject')->where($where)->delete();
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "成功";
            return json_encode($returnData);
        }  else{
            $returnData['status'] = 0;
            $returnData['msg'] = "失败";
            return json_encode($returnData);
        }
    }

}